package com.wdz.mvr.controllers;

import com.wdz.mvr.Services.ActorService;
import com.wdz.mvr.Services.ClassificationService;
import com.wdz.mvr.Services.MovieService;
import com.wdz.mvr.entities.Classification;
import com.wdz.mvr.entities.Movie;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("movies")
public class MovieController {

    @Autowired
    MovieService movieService;

    @Autowired
    ActorService actorService;

    @Autowired
    ClassificationService classificationService;

    @GetMapping
    public String displayMovies(Model model){
        List<Movie> movies = movieService.getAll();
        model.addAttribute("movies",movies);
        return "movies/list-movies";
    }

//    用于响应GET请求
    @GetMapping("/new")
    public String displayMovieForm(Model model){

        Movie aMovie = new Movie();
        model.addAttribute("movie",aMovie);

//        【电影--分类:一对多的方法】
        List<Classification> classifications = classificationService.getAll();
        model.addAttribute("allClassifications",classifications);
//        查询数据，保存到【电影分类】列表

        return "movies/new-movie";
    }

//    处理提交给服务器的POST请求

    @PostMapping("/save")
    public String createMovie(Model model, @RequestParam List<Long> classifications, Movie movie){

//        保存到数据库
        movieService.save(movie);

//        重定位防止重复提交
        return "redirect:/movies";
    }

    @GetMapping("/cat")
    public String displayCat(){
        return "movies/cat_one";
    }
}
